www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/models/Comment.php

    <?php

class Comment extends CActiveRecord
{
    const YES = 1;
    const NO = 0;
    const OPERATE_SUPPORT = 10;
    const OPERATE_OPPOSE = 20;
    const OPERATE_REPORT = 40;
    const OPERATE_NEUTRAL = 30;
    
	/**
	 * The followings are the available columns in table 'Comment':
	 * @var integer $id
	 * @var integer $post_id
	 * @var string $content
	 * @var integer $support_nums
	 * @var integer $oppose_nums
	 * @var integer $report_nums
	 * @var string $post_user
	 * @var integer $post_time
	 * @var string $post_ip
	 * @var string $post_user_email
	 * @var string $post_user_homepage
	 * @var integer $isshow
	 * @var integer $recommend
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Comment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('post_user','length', 'max'=>30),
			array('post_ip','length', 'max'=>15),
			array('post_user_email', 'length', 'max'=>50),
			array('post_user_email', 'email', 'allowEmpty' => true),
			array('post_user_homepage', 'length', 'max'=>50),
			array('post_id', 'required'),
			array('post_id, support_nums, oppose_nums, neutral_nums, report_nums, post_time, isshow, recommend', 'numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		    'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id'=>'ID',
			'post_id'=>'文章ID',
			'content'=>'内容',
			'support_nums'=>'支持',
			'oppose_nums'=>'反对',
			'report_nums'=>'举报',
		    'neutral_nums' => '中立',
			'post_user'=>'评论人',
			'post_time'=>'评论时间',
			'post_ip'=>'IP',
			'post_user_email'=>'邮箱',
			'post_user_homepage'=>'主页',
			'isshow'=>'显示',
			'recommend'=>'推荐',
		);
	}

	/**
	 * 获取评论人名称
	 * @return string 评论人,如果为空,则显示访客的名称
	 */
	public function getCommentUser()
    {
    	return empty($this->post_user) ? user()->guestName : $this->post_user;
    }
	
    
    /**
     * 处理过滤评论内容
     * @return string 过滤之后的评论内容
     */
    public function getPurifyContent()
    {
        // 使用 htmlpurifier过滤
        $html = CdcBetaTools::purify($this->content, 'comment');
        // 处理超级链接
        $html = CdcBetaTools::purifyLinkTag($html);
        // 过滤敏感字
        $html = CdcBetaTools::purifySpamWords($html);
        // 将连续空白字符修改为一个
        $p = '/(\s)+/im';
        $html = preg_replace($p, '$1', $html);
        
        return nl2br($html);
    }
    
    public function getWapContent()
    {
        $pattern = '/<fieldset.*<\/fieldset>/is';
        $content = preg_replace($pattern, '', $this->content);
        return ($content);
    }
    
    /**
     * 获取编辑推荐评论列表
     * @param $count integer 要获取的评论条数,默认20条
     * @return Comment 评论对象集合
     */
    public function getRecommendComment($count = 20)
    {
        $cacheId = param('cacheIdRecommendComment');
        param('caching') && $data = app()->cache->get($cacheId);
        if (!param('caching') || $data === false) {
            $criteria = $this->getDbCriteria();
        	$criteria->select = 'id, content, post_user, post_id';
            $criteria->limit = $count;
            $criteria->order = $this->tableName() . '.id desc';
            $criteria->condition = 'recommend = :yes and ' . $this->tableName() . '.isshow = :yes';
            $criteria->params = array('yes' => self::YES);
            $data = $this->with('post')->findAll($criteria);
            param('caching') && app()->cache->set($cacheId, $data, param('expireEditorRecommend'));
        }
        return $data;
    }
    
    
    public function beforeSave()
    {
        if ($this->isNewRecord) {
            $this->post_time = $_SERVER['REQUEST_TIME'];
            $this->post_ip = CdcBetaTools::getClientIp();
            $this->post_user = user()->isGuest ? '' : user()->getState('nickname');
            $this->isshow = self::YES;
        }
        return $this;
    }
    
    public function afterSave()
    {
        if ($this->isNewRecord) {
            $counters = array('comment_nums' => 1);
            Post::model()->updateCounters($counters, 'id = :pid', array('pid' => $this->post_id));
        }
        return $this;
    }
    
    public function afterDelete()
    {
        $counters = array('comment_nums' => -1);
        Post::model()->updateCounters($counters, 'id = :pid', array('pid' => $this->post_id));
        CommentMark::model()->deleteAllByAttributes(array('comment_id' => $this->id));
        return $this;
    }
}